import store from '@/store/index'
import { NoticeModel } from '@/store/newsCenter/types'
import { setNewsInfo } from '@/store/localstorage'
import alarmImg from '@/assets/image/newsCenter/alarm.png'
import systemImg from '@/assets/image/newsCenter/system.png'
import wayOrderImg from '@/assets/image/newsCenter/wayOrder.png'
import activityImg from '@/assets/image/newsCenter/activity.png'
export const sysList: any = {
  '0': {
    id: 0,
    title: '报警消息',
    memo: '',
    date: '',
    tipNum: 0,
    img: alarmImg
  },
  '1': {
    id: 1,
    title: '系统消息',
    memo: '',
    date: '',
    tipNum: 0,
    img: systemImg
  },
  '2': {
    id: 2,
    title: '运单消息',
    memo: '',
    date: '',
    tipNum: 0,
    img: wayOrderImg
  },
  '4': {
    id: 3,
    title: '活动消息',
    memo: '',
    date: '',
    tipNum: 0,
    img: activityImg
  }
}
// 获取未读通知列表
export const handleGetNoReadNotice = async (): Promise<any> => {
  const list = JSON.parse(JSON.stringify(sysList))
  const result = await store.dispatch('newsCenter/handleGetNoReadNotice', { type: 1, status: 0, isShow: true })
  if (result && result.length > 0) {
    result.forEach((item: NoticeModel) => {
      console.log(item.noticeType)
      if (item.noticeType === 5) {
        console.log(item, 'item.noticeType')
        item.noticeType = 0
      }
      const notice: any = list[String(item.noticeType)]
      if (notice) {
        list[String(item.noticeType)] = Object.assign(notice, {
          tipNum: (notice.tipNum += 1),
          date: notice.date < item.sysNoticeEntity.createTime ? item.sysNoticeEntity.createTime : notice.date,
          memo: notice.date < item.sysNoticeEntity.createTime ? item.sysNoticeEntity.content : notice.memo
        })
      }
    })
    console.log(list, 'list')
    setNewsInfo(list)
  } else {
    setNewsInfo({})
  }
  return list
}
// 获取通知列表
export const handleGetNotice = async (): Promise<any> => {
  const list = JSON.parse(JSON.stringify(sysList))
  const result = await store.dispatch('newsCenter/handleGetNotice', { type: 1, status: 0 })
  if (result.rows && result.rows.length > 0) {
    result.rows.forEach((item: NoticeModel) => {
      const notice: any = list[String(item.noticeType)]
      if (notice) {
        list[String(item.noticeType)] = Object.assign(notice, {
          tipNum: (notice.tipNum += 1),
          date: notice.date < item.sysNoticeEntity.createTime ? item.sysNoticeEntity.createTime : notice.date,
          memo: notice.date < item.sysNoticeEntity.createTime ? item.sysNoticeEntity.content : notice.memo
        })
      }
    })
    console.log(list, 'list')
    setNewsInfo(list)
  } else {
    setNewsInfo({})
  }
  return list
}
